-
Notifications
You must be signed in to change notification settings - Fork 55
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix class imports from node modules #165
Conversation
@tylerferrara I've labeled this "do not merge" for now, let me know when you're ready and I'll give it a review. Thanks. :-) |
Fantastic! Just letting you know I didn't forget about this Issue. The fix appears to work as intended, just designing tests to prove it for a variety of scenarios. Thank you for your patience 🙏 |
359c8c4
to
51bf38e
Compare
51bf38e
to
b275552
Compare
@rhys-vdw Ready for review |
Hey @tylerferrara, I'm going to be a bit busy until next year but I'll review then. Looks sensible from a superficial reading but I'd like to understand it all better (esp. the test framework) before I merge. Thank you for your detailed contribution and enjoy your new year. |
Hey @tylerferrara hope you had a nice NYE. This is actually a very nice short PR. Awesome stuff. Certainly a bounty well earned. Typically I offer anyone who contributes to the project collaborator status, but since you're a bounty hunter I'm not sure if this appeals? In any case the invite is there should you wish to take it. |
@tylerferrara throw a gitcoin submission whenever ready |
@wiegell thank you for your support of the project. ❤️ |
Fixes #162
How
import
statements are currently treatedLet's use this simple export as an example:
For every
export
found withinFoo.ts
, the ts-auto-guard library will generate an accompanying guard for the exported variable. Regardless of how many exports are found, all guards are placed within the guard file:Foo.guard.ts
.Each property of the exported variable is analyzed to determine its type. In our example above, we only have one exported variable
Foo
which has a single propertyoptions
. The expected guard file produced is as shown:NOTICE: There is no reference of
DirectoryAddOptions
within this guard file. That is because ts-auto-guard doesn't need it! It ONLY requires referencing the named type of a variable if it is a class. In our example, DirectoryAddOptions is an interface.The Issue : #162
If we instead change the options variable to a class type, like Directory, we encounter some problems.
As you can see, the guard file references the package
ts-morph
with a relative path within node_modules. This does not follow the TypeScript convention for non-relative package imports. In some cases, these relative imports do not correctly import the desired variable. This PR aims to address these problems with relative class imports.The Fix
This PR adds a check for each import statement being added to the guard file. If the accompanying import is found within the node modules directory, it will explicitly copy the reference found within the source file. For our above example, we grab the package reference
ts-morph
found inFoo.ts
. This solves our problem of referencing npm modules.One alternative method, which was NOT used, was to copy the entire import statement found within the source file. However, this was discouraged as it has the potential to include more variables than necessary within the guard file. For example, a file like:
will produce the guard file:
NOTICE: we import
DirectoryAddOptions
even though it is not used anywhere. This will raise an error for users with the TypeScript compiler option: noUnusedLocals. Therefore, I consider it a non-solution.Testing
The current tests use an inMemoryFileSystem for generating test projects. However, this does not adequately simulate a project containing node-modules. Therefore, I created another test file to auto generate a local file for every test. This file
ImportTest.ts
is created and destroyed for every test to eliminate contamination between tests. It's written into the local directory/tests
each time.Since it lives in the local file system, within the local TypeScript project, we can adequately test class imports from npm packages already found in the ts-auto-guard project. The use of the
ts-morph
package for tests was chosen because this project already heavily relies on ts-morph types. Therefore, it is highly unlikely to be removed in the future, preventing test failures.